using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using FlatRedBall.Localization;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
using FlatRedBall.Instructions;
#endif


using NereusGame.Entities.Ships.Enemies;
using NereusGame.Entities.Ships;
using NereusGame.Entities.HBodies;


namespace NereusGame.Screens
{
    public partial class Level1Sol
    {
        private int maxPlayers;
        private const int MAX_NERUTRAL_MINER = 5;
        private const int MAX_DEFENDER = 5;
        private const int MAX_ENENEMY_MINER = 15;
        private const int MAX_ASTEROIDS = 35;
        private const int MAX_ENEMIES = MAX_ENENEMY_MINER + MAX_NERUTRAL_MINER + MAX_DEFENDER + 1;


        void CustomInitialize()
        {
            InitializeWorld();
        }

        void CustomActivity(bool firstTimeCalled)
        {
            if (!InstructionManager.IsEnginePaused)
            {

                foreach (Ship s in Players)
                    s.Gravitate(Stars, Planets, g);

            }
        
        }

        void CustomDestroy()
        {
            if (Game1.mGameMode == Game1.GameMode.Arena)
            {
                SpriteManager.Cameras.Remove(Players[1].Cameras);
            }
            base.Destroy();

        }

        static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        //*****************************************************INITIALIZE*****************************************************************//
        private void InitializeWorld()
        {

            backGround.Texture = BackGround;
            backGround.Z = -25;
            backGround.ScaleX = 400;
            backGround.ScaleY = 400;
            InitializePlayers();
            InitializeAsteroids();
            InitializeEnemies();
            InitializeProjectiles();

            Stars = new List<Star>();
            Star Sun = new Star(CONTEN_MANAGER);
            Stars.Add(Sun);

            Planets = new List<Planet>();
            Planet Mercury = new Planet(CONTEN_MANAGER);
            Planets.Add(Mercury);
            Mercury.setDistanceFromStar(22, Sun.getMass(), g);

            Planet Venus = new Planet(CONTEN_MANAGER);
            Planets.Add(Venus);
            Venus.setDistanceFromStar(50, Sun.getMass(), g);

            Planet Earth = new Planet(CONTEN_MANAGER);
            Planets.Add(Earth);
            Earth.setDistanceFromStar(100, Sun.getMass(), g);

            Planet Mars = new Planet(CONTEN_MANAGER);
            Planets.Add(Mars);
            Mars.setDistanceFromStar(160, Sun.getMass(), g);
        }

        private void InitializeProjectiles()
        {
            ActiveProjectiles = new List<Entities.Projectiles.Projectile>(50 * (MAX_ENEMIES + 3));
            InactiveProjectiles = new List<Entities.Projectiles.Projectile>(50 * (MAX_ENEMIES + 3));

            for (int i = 0; i < maxPlayers; i++)
            {
                for (int j = 0; j < Players[i].weaponInventory.Length; j++)
                {
                    for (int k = 0; k < Players[i].weaponInventory[j].deadAmmo.Count; k++)
                    {
                        InactiveProjectiles.Add(Players[i].weaponInventory[j].deadAmmo.Peek());
                        Players[i].weaponInventory[j].deadAmmo.Enqueue(Players[i].weaponInventory[j].deadAmmo.Dequeue());

                    }
                }
            }



            for (int i = 0; i < MAX_ENEMIES; i++)
            {
                for (int j = 0; j < Enemies[i].weaponInventory.Length; j++)
                {
                    for (int k = 0; k < Enemies[i].weaponInventory[j].deadAmmo.Count; k++)
                    {
                        InactiveProjectiles.Add(Enemies[i].weaponInventory[j].deadAmmo.Peek());
                        Enemies[i].weaponInventory[j].deadAmmo.Enqueue(Enemies[i].weaponInventory[j].deadAmmo.Dequeue());

                    }
                }
            }
        }

        private void InitializePlayers()
        {

            Players = new List<Player>();

            if (Game1.mGameMode == Game1.GameMode.Arena)
            {
                maxPlayers = 2;
                for (int i = 1; i < maxPlayers; i++)
                {
                    SpriteManager.Cameras.Add(new Camera(CONTEN_MANAGER));
                }
                SpriteManager.Cameras[0].SetSplitScreenViewport(Camera.SplitScreenViewport.LeftHalf);
      
                SpriteManager.Cameras[1].SetSplitScreenViewport(Camera.SplitScreenViewport.RightHalf);
        

                for (int i = 0; i < maxPlayers; i++)
                {
                    Players.Add(new Player(CONTEN_MANAGER));
                    Players[i].Position = new Vector3(backGround.ScaleX / 2f, backGround.ScaleY / 4, 0);
                    Players[i].Position.X *= -1;
                    Players[i].SetCamera(SpriteManager.Cameras[i]);
                    Players[i].PlayerIndex = i;

                }
                

            }
            else
            {
                maxPlayers = 1;
                Players.Add(new Player(CONTEN_MANAGER));
                Players[0].SetPlayerWithCamera(new Vector3(0, backGround.ScaleY / 4, 0));




            }


        }

        private void InitializeAsteroids()
        {
            Random rand = new Random();
            float minRange = 220;
            float maxRange = 280;
            float angle;
            float distance;

            Asteroids = new List<Asteroid>(MAX_ASTEROIDS);
            Asteroid newAsteroid;
            for (int i = 0; i < MAX_ASTEROIDS; i++)
            {
                newAsteroid = new Asteroid(CONTEN_MANAGER);
                Asteroids.Add(newAsteroid);
                angle = (float)(i * 2 * Math.PI / MAX_ASTEROIDS) + (float)(rand.Next(-10, 10) * Math.PI / MAX_ASTEROIDS);
                distance = minRange + rand.Next(0, (int)(maxRange - minRange));
                newAsteroid.Position.Y = (float)Math.Sin(angle) * distance;
                newAsteroid.Position.X = (float)Math.Cos(angle) * distance;
            }

           // SetAsteroids();
        }

        private void SetAsteroids()
        {
            Asteroids[0].Position.X = backGround.ScaleX / 3.5f;
            Asteroids[0].Position.Y = backGround.ScaleY / 4f;

            Asteroids[1].Position.X = backGround.ScaleX / 3f;
            Asteroids[1].Position.Y = -backGround.ScaleY / 3f;

            Asteroids[2].Position.X = -backGround.ScaleX / 4f;
            Asteroids[2].Position.Y = backGround.ScaleY / 2f;

            Asteroids[3].Position.X = -backGround.ScaleX / 3.5f;
            Asteroids[3].Position.Y = -backGround.ScaleY / 2.3f;
        }


        private void InitializeEnemies()
        {
            Enemies = new List<Enemy>(MAX_ENEMIES);

            for (int i = 0; i < MAX_ENENEMY_MINER; i++)
            {
                Enemies.Add(new EnemyMiner(CONTEN_MANAGER));
                Enemies[i].SetSecondaryTarget(Players[0]);
                if (Game1.mGameMode == Game1.GameMode.Arena)
                    Enemies[i].SetTertieryTarget(Players[1]);

            }

            for (int i = 0; i < MAX_NERUTRAL_MINER; i++)
            {
                NeutralMiner temp = new NeutralMiner(CONTEN_MANAGER);
                temp.SetAsteroidsList(Asteroids);
                Enemies.Add(temp);
            }

            for (int i = 0; i < MAX_DEFENDER; i++)
            {
                EnemyDefender temp = new EnemyDefender(CONTEN_MANAGER);
                temp.SetSecondaryTarget(Players[0]);
                if (Game1.mGameMode == Game1.GameMode.Arena)
                    temp.SetTertieryTarget(Players[1]);
                Enemies.Add(temp);

            }

            // boss

            EnemyBoss temp1 = new EnemyBoss(CONTEN_MANAGER);
            temp1.SetPrimaryTarget(Players[0]);
            Enemies.Add(temp1);

            SetEnemies();




        }



        private void SetEnemies()
        {
            SetEnemyMiners();
            SetNeutralMiners();
            SetDefenders();


        }

        private void SetEnemyMiners()
        {
            Enemies[0].Position.X = Asteroids[0].Position.X + Asteroids[0].Body.Radius + 18f;
            Enemies[0].Position.Y = Asteroids[0].Position.Y + Asteroids[0].Body.Radius;
            Enemies[0].SetPrimaryTarget(Asteroids[0]);



            Enemies[1].Position.X = Asteroids[0].Position.X - Asteroids[0].Body.Radius;
            Enemies[1].Position.Y = Asteroids[0].Position.Y - Asteroids[0].Body.Radius + 14f;
            Enemies[1].SetPrimaryTarget(Asteroids[0]);




            Enemies[2].Position.X = Asteroids[1].Position.X + Asteroids[1].Body.Radius + 17f;
            Enemies[2].Position.Y = Asteroids[1].Position.Y + Asteroids[1].Body.Radius;
            Enemies[2].SetPrimaryTarget(Asteroids[1]);




            Enemies[3].Position.X = Asteroids[1].Position.X - Asteroids[1].Body.Radius - 20f;
            Enemies[3].Position.Y = Asteroids[1].Position.Y + Asteroids[1].Body.Radius + 7f;
            Enemies[3].SetPrimaryTarget(Asteroids[1]);



            Enemies[4].Position.X = Asteroids[2].Position.X + Asteroids[2].Body.Radius + 17f;
            Enemies[4].Position.Y = Asteroids[2].Position.Y + Asteroids[2].Body.Radius;
            Enemies[4].SetPrimaryTarget(Asteroids[2]);




            Enemies[5].Position.X = Asteroids[2].Position.X - Asteroids[2].Body.Radius - 20f;
            Enemies[5].Position.Y = Asteroids[2].Position.Y + Asteroids[2].Body.Radius + 7f;
            Enemies[5].SetPrimaryTarget(Asteroids[2]);



            Enemies[6].Position.X = Asteroids[2].Position.X + Asteroids[2].Body.Radius + 20f;
            Enemies[6].Position.Y = Asteroids[2].Position.Y + Asteroids[2].Body.Radius + 7f;
            Enemies[6].SetPrimaryTarget(Asteroids[2]);

        }

        private void SetNeutralMiners()
        {
            Enemies[7].Position.X = Asteroids[3].Position.X + Asteroids[3].Body.Radius + 20f;
            Enemies[7].Position.Y = Asteroids[3].Position.Y + Asteroids[3].Body.Radius + 7f;
            Enemies[7].SetPrimaryTarget(Asteroids[3]);


            Enemies[8].Position.X = Asteroids[3].Position.X - Asteroids[3].Body.Radius - 20f;
            Enemies[8].Position.Y = Asteroids[3].Position.Y + Asteroids[3].Body.Radius + 7f;
            Enemies[8].SetPrimaryTarget(Asteroids[3]);

            Enemies[9].Position.X = Asteroids[3].Position.X + Asteroids[3].Body.Radius + 17f;
            Enemies[9].Position.Y = Asteroids[3].Position.Y + Asteroids[3].Body.Radius;
            Enemies[9].SetPrimaryTarget(Asteroids[3]);

        }


        private void SetDefenders()
        {
            Enemies[10].Position.X = Asteroids[0].Position.X - Asteroids[0].Body.Radius - 20f;
            Enemies[10].Position.Y = Asteroids[0].Position.Y + Asteroids[0].Body.Radius + 7f;
            Enemies[10].SetPrimaryTarget(Asteroids[0]);



            Enemies[11].Position.X = Asteroids[1].Position.X + Asteroids[1].Body.Radius + 20f;
            Enemies[11].Position.Y = Asteroids[1].Position.Y + Asteroids[1].Body.Radius + 7f;
            Enemies[11].SetPrimaryTarget(Asteroids[1]);

            Enemies[12].Position.X = Asteroids[2].Position.X - Asteroids[2].Body.Radius - 17f;
            Enemies[12].Position.Y = Asteroids[2].Position.Y + Asteroids[2].Body.Radius;
            Enemies[12].SetPrimaryTarget(Asteroids[2]);

        }
    }
}
